package com.zyd.shiro.controller.admin;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.zyd.shiro.persistence.beans.TrainUser;
import com.zyd.shiro.persistence.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zyd.shiro.persistence.beans.TrainMeeting;
import com.zyd.shiro.business.service.MallTrainMeetingService;

import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.ResultUtil;
import sun.util.calendar.LocalGregorianCalendar;

/**
 * 控制器层
 * @author
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/trainMeeting")
public class TrainMeetingController {

	@Autowired
	private MallTrainMeetingService trainMeetingService;

	/**
	 * 查询全部数据,已测试
	 * @return 返回所有数据
	 */
	@RequestMapping(value="/findAll/",method=RequestMethod.GET)
	public ResponseVO findAll(){
		return ResultUtil.success("查询成功",trainMeetingService.findAll());
	}


	/**后台查看所有会议
	 *
	 */
	@RequestMapping(value="/findAllMeetingDESC/",method=RequestMethod.GET)
	public ResponseVO findAllMeetingDESC() throws ParseException {
		return ResultUtil.success("查询成功",trainMeetingService.findAllDESC(0));
	}
	/**后台查看所有培训
	 *
	 */
	@RequestMapping(value="/findAllTrainDESC/",method=RequestMethod.GET)
	public ResponseVO findAllTrainDESC() throws ParseException {
		return ResultUtil.success("查询成功",trainMeetingService.findAllDESC(1));
	}

	/**
	 *查询时间大于现在时间的所有会议（即meeting_Date>nowDate）,按时间顺序排列，已测试
	 * @return 按时间顺序返回有效时间的所有会议
	 */
	@RequestMapping(value="/findMeetingASC/",method=RequestMethod.GET)
	public ResponseVO findAllMeetingNeedToShowASC() throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功",trainMeetingService.findAllNeedShowASC(date,0));
	}
	/**
	 *查询时间大于现在时间的所有会议（即meeting_Date>nowDate）,按时间逆序排列，已测试
	 * @return 按时间逆序返回有效时间的所有会议
	 */
	@RequestMapping(value="/findMeetingDESC/",method=RequestMethod.GET)
	public ResponseVO findAllMeetingNeedToShowDESC() throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功",trainMeetingService.findAllNeedShowDESC(date,0));
	}
	/**
	 *查询时间大于现在时间的所有培训（即meeting_Date>nowDate）,按时间顺序排列，已测试
	 * @return 按时间顺序返回有效时间的所有培训
	 */
	@RequestMapping(value="/findTrainASC/",method=RequestMethod.GET)
	public ResponseVO findAllTrainNeedToShowASC() throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功",trainMeetingService.findAllNeedShowASC(date,1));
	}
	/**
	 *查询时间大于现在时间的所有培训（即meeting_Date>nowDate）,按时间逆序排列，已测试
	 * @return 按时间顺序返回有效时间的所有培训
	 */
	@RequestMapping(value="/findTrainDESC/",method=RequestMethod.GET)
	public ResponseVO findAllTrainNeedToShowDESC() throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功",trainMeetingService.findAllNeedShowDESC(date,1));
	}



	/**
	 * 根据主题（theme）分类查询,顺序，查询有效会议,已测试
	 */
	@RequestMapping(value="/findMeetingByThemeASC/",method=RequestMethod.GET)
	public ResponseVO findAllMeetingByThemeASC(@RequestBody  Map themeMap) throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功", trainMeetingService.findAllByThemeASC(themeMap.get("theme").toString(),date,0));
	}
	/**
	 * 根据主题（theme）分类查询,逆序，查询有效会议,已测试
	 */
	@RequestMapping(value="/findMeetingByThemeDESC/",method=RequestMethod.GET)
	public ResponseVO findAllMeetingByThemeDESC(@RequestBody  Map themeMap) throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功", trainMeetingService.findAllByThemeDESC(themeMap.get("theme").toString(),date,0));
	}
	/**
	 * 根据主题（theme）分类查询,顺序，查询有效培训,已测试
	 */
	@RequestMapping(value="/findTrainByThemeASC/",method=RequestMethod.GET)
	public ResponseVO findAllTrainByThemeASC(@RequestBody  Map themeMap) throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功", trainMeetingService.findAllByThemeASC(themeMap.get("theme").toString(),date,1));
	}
	/**
	 * 根据主题（theme）分类查询,逆序，查询有效培训,已测试
	 */
	@RequestMapping(value="/findTrainByThemeDESC/",method=RequestMethod.GET)
	public ResponseVO findAllTrainByThemeDESC(@RequestBody  Map themeMap) throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功", trainMeetingService.findAllByThemeDESC(themeMap.get("theme").toString(),date,1));
	}



	/**
	 * 分类查询:根据地点 顺序查询有效会议,已测试
	 */
	@RequestMapping(value="/findMeetingByPlaceASC/",method=RequestMethod.GET)
	public ResponseVO findAllMeetingByPlaceASC(@RequestBody  Map placeMap) throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功", trainMeetingService.findAllByPlaceASC(placeMap.get("place").toString(),date,0));
	}
	/**
	 * 分类查询:根据地点 逆序查询有效会议,已测试
	 */
	@RequestMapping(value="/findMeetingByPlaceDESC/",method=RequestMethod.GET)
	public ResponseVO findAllMeetingByPlaceDESC(@RequestBody  Map placeMap) throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功", trainMeetingService.findAllByPlaceDESC(placeMap.get("place").toString(),date,0));
	}
	/**
	 * 分类查询:根据地点 顺序查询有效培训,已测试
	 */
	@RequestMapping(value="/findTrainByPlaceASC/",method=RequestMethod.GET)
	public ResponseVO findAllTrainByPlaceASC(@RequestBody  Map placeMap) throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功", trainMeetingService.findAllByPlaceASC(placeMap.get("place").toString(),date,1));
	}
	/**
	 * 分类查询:根据地点 逆序查询有效培训,已测试
	 */
	@RequestMapping(value="/findTrainByPlaceDESC/",method=RequestMethod.GET)
	public ResponseVO findAllTrainByPlaceDESC(@RequestBody  Map placeMap) throws ParseException {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(d);
		Date date = sdf.parse(time);
		return ResultUtil.success("查询成功", trainMeetingService.findAllByPlaceDESC(placeMap.get("place").toString(),date,1));
	}

        //根据名字可以查询到过期的会议或者培训
	/**
	 * 根据标题,顺序查询所有会议,已测试
	 */
	@RequestMapping(value="/findMeetingByTitleASC/",method=RequestMethod.GET)
	public ResponseVO findAllMeetingByTitleASC(@RequestBody Map titleMap){
		return ResultUtil.success("查询成功", trainMeetingService.findAllByTitleASC(titleMap.get("title").toString(),0));
	}
	/**
	 * 根据标题,逆序查询所有会议,已测试
	 */
	@RequestMapping(value="/findMeetingByTitleDESC/{title}",method=RequestMethod.GET)
	public ResponseVO findAllMeetingByTitleDESC(@PathVariable String title){
		return ResultUtil.success("查询成功", trainMeetingService.findAllByTitleDESC(title,0));
	}
	/**
	 * 根据标题,顺序查询所有培训,已测试
	 */
	@RequestMapping(value="/findTrainByTitleASC/",method=RequestMethod.GET)
	public ResponseVO findAllTrainByTitleASC(@RequestBody Map titleMap){
		return ResultUtil.success("查询成功", trainMeetingService.findAllByTitleASC(titleMap.get("title").toString(),1));
	}
	/**
	 * 根据标题,逆序查询所有培训,已测试
	 */
	@RequestMapping(value="/findTrainByTitleDESC/{title}",method=RequestMethod.GET)
	public ResponseVO findAllTrainByTitleDESC(@PathVariable String title){
		return ResultUtil.success("查询成功", trainMeetingService.findAllByTitleDESC(title,1));
	}


	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/findByID/{id}",method=RequestMethod.GET)
	public ResponseVO findOne(@PathVariable Integer id){
		return ResultUtil.success("查询成功", trainMeetingService.findOne(id));

	}



	/**
	 * 分页查询全部数据
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/{page}/{size}/{type}",method=RequestMethod.GET)
	public ResponseVO findPage(@PathVariable int page,@PathVariable int size,@PathVariable int type){
		Page<TrainMeeting> pageList = trainMeetingService.findPage(page, size,type);
		return ResultUtil.success("查询成功",new PageResult(pageList.getTotalElements(), pageList.getContent() ) );
	}

	/**
	 * 分页+多条件查询 
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/{page}/{size}/{searchMap}/{type}",method=RequestMethod.POST)
	public ResponseVO findSearch(@PathVariable String searchMap ,@PathVariable int page,@PathVariable int size,@PathVariable int type){
		Page<TrainMeeting> pageList = trainMeetingService.findSearch(searchMap, page, size,type);
		return ResultUtil.success("查询成功",  new PageResult(pageList.getTotalElements(), pageList.getContent()) );
	}
	/**
	 * 增加主题，已测试
	 * @param
	 */
	@RequestMapping(value="/addTheme/{theme}",method=RequestMethod.POST)
	public ResponseVO addTheme(@PathVariable String theme )  {
		if(trainMeetingService.IshaveTheme(theme)==0)
		{
			trainMeetingService.addTheme(theme);
			return ResultUtil.success("增加成功");
		}else {
			return ResultUtil.error("已存在");
		}
	}
	/**
	 * 查询主题
	 */
	@RequestMapping(value="/findThemeDESC/",method=RequestMethod.GET)
	public ResponseVO findAllTrainByTitleDESC(){
		return ResultUtil.success("查询成功", trainMeetingService.findAllTheme());
	}


	/**
	 * 增加会议，已测试
	 * @param trainMeeting
	 */
	@RequestMapping(value="/addMeeting/",method=RequestMethod.POST)
	public ResponseVO addMeeting(@RequestBody TrainMeeting trainMeeting  ) throws ParseException {

		Date d = trainMeeting.getMeeting_date();
		trainMeeting.setMeeting_date(d);
		trainMeeting.setMeeting_type(0);
		trainMeeting.setTrain_price(0);
		trainMeeting.setTrain_maxnum(0);
		Date nowdate = new Date();
		trainMeeting.setCreate_time(nowdate);
		trainMeetingService.add(trainMeeting);
		return ResultUtil.success("增加成功");
	}
	/**增加培训,已测试
	 *
	 */
	@RequestMapping(value="/addTrain/",method=RequestMethod.POST)
	public ResponseVO addTrain(@RequestBody TrainMeeting trainMeeting  ) throws ParseException {

		Date d = trainMeeting.getMeeting_date();
		trainMeeting.setMeeting_date(d);
		trainMeeting.setMeeting_type(1);
		Date nowdate = new Date();
		trainMeeting.setCreate_time(nowdate);
		trainMeetingService.add(trainMeeting);
		return ResultUtil.success("增加成功");
	}

	/**
	 * 修改,已测试
	 * @param trainMeeting
	 */
	@RequestMapping(value="/update/{id}",method=RequestMethod.PUT)
	public ResponseVO update(@RequestBody TrainMeeting trainMeeting,@PathVariable Integer id ) throws ParseException {

		trainMeeting.setId(id);
		trainMeetingService.update(trainMeeting);
		return ResultUtil.success("修改成功");
	}



	/**
	 * 删除，已测试
	 * @param id
	 */
	@RequestMapping(value="/delete/{id}",method=RequestMethod.DELETE)
	public ResponseVO delete(@PathVariable Integer id ){
		trainMeetingService.delete(id);
		return ResultUtil.success("删除成功");
	}

}
